<template>
    <div class="article-layout">
        <div class="article-main">
            <header class="header">
                <div class="left">
                    <img src="/src/assets/logo.png" alt="logo" width="150" @click="goHome">
                    <div class="search-bar-wrap search-bar-light pc-header-search">
                        <div class="adm-input search-bar-input"><input class="adm-input-element" v-model="searchText"
                                placeholder="Enter your keywords" @keyup.enter="handleSearch"></div><span
                            class="search-icon" @click="handleSearch"></span>
                    </div>
                </div>
                <div class="pc-header-extra">
                    <div class="dropdown pc-header-extra-item">
                        <div class="dropdown-title"><span>Categories</span></div>
                        <div class="dropdown-list">
                            <p class="dropdown-list-item" @click="goArticle(item)" v-for="(item, index) in category"
                                :key="index">{{ item?.alias }}</p>
                        </div>
                    </div><span class="dropdown-title" @click="showFeedBack"><span
                            class="pc-feedback-text">Feedback</span></span>
                </div>
            </header>

            <main class="main">
                <div class="category-page-title">{{ title }}</div>

                <div class="main-page">
                    <div class="page-layout-main">
                        <div class="category-card" v-for="(item, index) in data" :key="index">
                            <a class="category-card-img-wrap" :href="`/detail/${item.id}`"><img
                                    class="category-card-img" :src="item?.image" alt="poster"></a>
                            <div class="category-card-info">
                                <div>

                                    <h3 class="category-card-title ellipsis-2"><a :href="`/detail/${item.id}`">{{
                                        item.title }}</a></h3>
                                    <p class="category-card-cnt ellipsis-2"><a :href="`/detail/${item.id}`">{{
                                        item?.subTitle }}</a></p>
                                </div>
                            </div>
                        </div>
                        <!-- <nav v-if="data?.length > 0">
                            <ul class="pagination">
                                <li class="page-item" @click="goBack">
                                    «
                                </li>

                                <li :class="['page-item', (pageNum == index + 1) && 'active']"
                                    v-for="(item, index) in totalPage" :key="index" @click="goPage(index + 1)">
                                    {{ index + 1 }}
                                </li>

                                <li class="page-item" @click="goNext">
                                    »
                                </li>
                            </ul>
                        </nav> -->
                    </div>
                    <RecentPost />
                </div>
            </main>

            <footer class="footer">
                <p><a style="text-decoration: none;color: #aaa;" href="/articles/769.html">Imprint</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/768.html">Privacy Policy</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/767.html">Terms of Service</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/770.html">Contact Us</a>
                </p>
                <p style="margin-top: 12px;">© 2024 - 2025 OhWellMind All Rights Reserved.</p>
            </footer>
        </div>
    </div>
</template>

<script setup lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
defineComponent({ name: 'Article' })
import RecentPost from '../components/recentPost.vue'
import { useRoute, useRouter } from 'vue-router'
import { getAllCategory, getArticlePage } from '../api/api';
import { ElMessage, ElMessageBox } from 'element-plus';
const router = useRouter()
const route = useRoute()
const title = ref('')
const searchText = ref<string>('')
const category = ref([])
const data = ref([])
const totalPage = ref()
const pageNum = ref(1)
const goHome = () => {
    router.push('/')
}
const goArticle = (item) => {
    router.push({
        name: 'Article',
        params: { id: item?.id },
        query: { title: item.alias }
    })
}
const handleSearch = () => {
    if (!searchText.value) return
    router.push({
        name: 'SearchPage',
        query: { search: searchText.value }
    })
}

const getCategory = () => {
    getAllCategory()
        .then(response => {
            category.value = response.data?.filter((item) => item.id === 108 || item.id === 111 || item.id === 112 || item.id === 113 || item.id === 120 || item.id === 121)
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

const getArticleData = () => {
    getArticlePage({ categoryId: route?.params?.id as string || null, pageNum: pageNum.value, pageSize: 10, search: searchText.value || null })
        .then(response => {
            data.value = response.data?.records
            totalPage.value = response?.data?.pages
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

const goBack = () => {
    if (pageNum.value === 1) return
    pageNum.value = pageNum.value - 1
    getArticleData()
}
const goPage = (page: number) => {
    pageNum.value = page
    getArticleData()
}
const goNext = () => {
    if (pageNum.value === totalPage.value) return
    pageNum.value = pageNum.value + 1
    getArticleData()
}

const showFeedBack = () => {
    ElMessageBox.prompt('Please input your feedback', 'Feedback', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
    })
        .then(({ value }) => {
            ElMessage({
                type: 'success',
                message: `THANK YOU`,
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Input canceled',
            })
        })
}


onMounted(() => {
    title.value = route.query.title as string
    searchText.value = route.query.search as string
    document.body.style.backgroundColor = '#000'
    getCategory()
    getArticleData()
})

watch(() => route.params.id, () => {
    getArticleData()
});

watch(() => route.query.title, () => {
    title.value = route.query.title as string
});

</script>

<style lang="scss" scoped>
.article-layout {
    background-color: #000;
    min-height: 100vh;

    .article-main {
        max-width: 1200px;
        margin: auto;

        .header {
            height: 100px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24px 20px;
            text-align: center;
            color: #fff;

            .search-bar-light {
                background-color: #334155;
                border: 1px solid hsla(0, 0%, 100%, .4)
            }

            .search-bar-wrap {
                display: flex;
                align-items: center;
                width: calc(100% - 6px);
                max-width: 748px;
                height: 44px;
                padding: 9px 12px;
                margin: 40px auto 30px;
                border-radius: 10px;
            }

            .search-bar-light .adm-input-element {
                color: #fff;
            }

            .pc-header-search {
                display: none;
                width: 482px;
                margin: 0 0 0 22px;

                .adm-input {
                    --font-size: var(--adm-font-size-9);
                    --color: var(--adm-color-text);
                    --placeholder-color: var(--adm-color-light);
                    --text-align: left;
                    --background-color: transparent;
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    width: 100%;
                    max-width: 100%;
                    max-height: 100%;
                    min-height: 24px;
                    background-color: var(--background-color);

                }

                .adm-input-element {
                    flex: auto;
                    display: inline-block;
                    box-sizing: border-box;
                    width: 100%;
                    max-width: 100%;
                    max-height: 100%;
                    padding: 0;
                    margin: 0;
                    font-size: var(--font-size);
                    line-height: 1.5;
                    background: transparent;
                    border: 0;
                    outline: none;
                    -webkit-appearance: none;
                    appearance: none;
                    min-height: 1.5em;
                    text-align: var(--text-align);
                    font-style: italic;
                }


                .search-bar-input {
                    height: 30px;
                    line-height: 30px;
                    font-size: 16px;
                    border: none;
                }

                .search-icon {
                    width: 18px;
                    height: 18px;
                    margin-left: 6px;
                    background: no-repeat url(https://6gamehub.com/static/img/bestreplyai/search-icon.webp);
                    background-size: 18px 18px;
                    cursor: pointer;
                }

            }



            @media screen and (min-width: 765px) {
                .pc-header-search {
                    display: flex;
                }
            }

            .left {
                display: flex;
                align-items: center;
                gap: 10px;
            }



            .pc-header-extra {
                display: flex;

                .dropdown-title {
                    padding: 6px 10px;
                    background: rgba(0, 0, 0, .3);
                    border-radius: 8px;
                    line-height: 18px;
                    cursor: pointer;
                    color: #fff;
                }

                .pc-header-extra-item {
                    margin-right: 20px;
                }

                .pc-header-extra-item .dropdown-list {
                    min-width: 120px;
                }

                .pc-header-extra-item .dropdown-list-item {
                    text-transform: capitalize;
                }

                .dropdown {
                    position: relative;
                    cursor: pointer;

                    .dropdown-list {
                        overflow: hidden;
                        position: absolute;
                        display: none;
                        width: 100%;
                        height: 0;
                        left: -8px;
                        z-index: 999;
                        padding-top: 10px;
                        color: #fff;
                        background: transparent;
                        transition: height 1s;
                        border-radius: 0 0 8px 8px;

                        .dropdown-list-item {
                            padding: 12px;
                            text-align: center;
                            font-size: 16px;
                            line-height: 16px;
                            cursor: pointer;
                            background-color: rgba(128, 128, 128, 0.6);
                        }

                        .dropdown-list-item:hover {
                            background-color: #246def;
                        }

                        .dropdown-list-item:first-child {
                            border-radius: 8px 8px 0 0;
                        }
                    }
                }

                .dropdown:hover .dropdown-list {
                    display: block;
                    height: auto;
                }
            }

            .pc-feedback {
                cursor: pointer;
            }


        }

        .main {
            margin-top: 10px;
            padding: 24px 20px;
            min-height: calc(100vh - 190px);

            .category-page-title {
                margin: 40px 0;
                color: #fff;
                font-weight: 700;
                font-size: 32px;
                text-align: center;
            }

            .main-page {
                display: flex;
                justify-content: space-between;
                gap: 15px;

                .page-layout-main {
                    flex: 1 1;
                    max-width: 765px;
                    min-width: 300px;

                    .category-card {
                        display: flex;
                        padding: 14px 0;
                        border-bottom: 1px solid hsla(0, 0%, 92.2%, .2);
                    }

                    .category-card-img-wrap {
                        width: 220px;
                        cursor: pointer;
                    }

                    .category-card-img {
                        width: 100%;
                        height: 100%;
                        border-radius: 6px;
                    }

                    .category-card-info {
                        display: flex;
                        flex-direction: column;
                        justify-content: space-between;
                        flex: 1 1;
                        margin-left: 16px;

                        .category-card-category {
                            margin-bottom: 6px;
                            font-weight: 500;
                            font-size: 12px;
                            line-height: 14px;
                        }


                        .category-card-category a {
                            background-color: #e261e5;
                            display: inline-block;
                            height: 16px;
                            padding: 0 5px;
                            line-height: 16px;
                            font-size: 10px;
                            color: #000;
                            border-radius: 8px;

                        }

                        .ellipsis-2 {
                            overflow: hidden;
                            display: -webkit-box;
                            text-overflow: ellipsis;
                            -webkit-box-orient: vertical;
                            -webkit-line-clamp: 2;
                        }

                        .category-card-title {
                            margin: 0 0 10px;
                            line-height: 30px;
                            font-weight: 600;
                            font-size: 26px;
                        }

                        .category-card-cnt {
                            font-weight: 400;
                            font-size: 16px;
                            line-height: 19px;
                        }

                        .category-card-title a {
                            color: #fff;

                        }

                        .category-card-cnt a {
                            color: #ccc;
                        }

                        a {
                            text-decoration: none;
                            transition: opacity .2s ease-in-out;
                        }
                    }
                }

                .main-article {
                    max-width: 765px;
                    min-width: 300px;
                }


                @media screen and (min-width: 765px) {
                    .page-layout .page-layout-main {
                        margin-right: 100px;
                    }
                }
            }

            .pagination {
                margin-top: 10px;
                display: -ms-flexbox;
                display: flex;
                padding-left: 0;
                list-style: none;
                border-radius: .25rem;

                .page-item:first-child .page-link {
                    margin-left: 0;
                    border-top-left-radius: .25rem;
                    border-bottom-left-radius: .25rem;
                }

                .page-item:last-child .page-link {
                    border-top-right-radius: .25rem;
                    border-bottom-right-radius: .25rem;
                }

                .page-item {
                    cursor: pointer;
                    position: relative;
                    display: block;
                    padding: .5rem .75rem;
                    margin-left: -1px;
                    line-height: 1.25;
                    color: #007bff;
                    background-color: #fff;
                    border: 1px solid #dee2e6;
                }

                .page-item.active {
                    z-index: 3;
                    color: #fff;
                    background-color: #007bff;
                    border-color: #007bff;
                }


            }


        }

        .footer {
            color: #aaa;
            margin: 0 auto;
            padding: 20px 0;
            font-size: 14px;
            line-height: 13px;
            text-align: center;
        }

    }



}
</style>